﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    /// <summary>
    /// AuditHandler 的摘要说明
    /// </summary>
    public class AuditHandler : IHttpHandler
    {



        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string parentId = context.Request.Params["ID"];
            string batchId = "";
            string projectId = "";
            string contractId = "";
            //Honestysoft.Financing.BusinessLogic.Contract contractBLL = new BusinessLogic.Contract("");
           //AuditUtils.ContractTreeModule treeModule = (Utils.ContractTreeModule)int.Parse(context.Request.Params[Utils.ParamName_ContractTreeModule]);
            List<Honestysoft.Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode> lsNode = new List<Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode>();
            string responseData = "";
            Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode node = null;
            //List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsProjectCategoryDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory");
            //List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsContractCategoryDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ContractCategory");
            string pageUrl = "";

            if (parentId == null) //根未加载
            {                
                //得到批次
                OrderParameters order = new OrderParameters();
                order.Add("BatchNo", OrderParameters.OrderDirection.Desc);
                DataTable dtBatch = new BaseBusinessLogic<Bu_AuditBatch>().GetTable(null, order);
                DataTable dtBatchProject = new Honestysoft.Financing.BusinessLogic.Audit("").GetBatchProject();
                if (dtBatch != null && dtBatch.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtBatch.Rows)
                    {
                        node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();                        
                        node.ID = dr["ID"].ToString();
                        node.Text ="（"+ dr["BatchNo"]+"）第"+ChineseNum.GetChineseNum(dr["BatchNo"].ToString())+"批";
                        node.ChildCount = dtBatchProject.Select("BatchID='" + node.ID + "'").Length;
                        node.NodeAttributes = new Dictionary<string, string>();
                        node.NodeAttributes.Add("BatchID", node.ID);
                        node.NodeAttributes.Add("URL", "Bu_AuditTable_List.aspx?ID=" + node.ID+"&BatchID="+node.ID);
                        node.NodeAttributes.Add(AuditUtils.ParamName_AuditNodeType, ((int)AuditUtils.AuditTreeNodeType.Batch).ToString());
                        lsNode.Add(node);
                    }
                }                
            }
            else
            {
                AuditUtils.AuditTreeNodeType nodeType = (AuditUtils.AuditTreeNodeType)int.Parse(context.Request.Params[AuditUtils.ParamName_AuditNodeType]);
                if (nodeType == AuditUtils.AuditTreeNodeType.Batch)//批次节点，加载项目
                {
                    DataTable dtBatchProject = new Honestysoft.Financing.BusinessLogic.Audit("").GetBatchProject(parentId);
                    if (dtBatchProject != null && dtBatchProject.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtBatchProject.Rows)
                        {
                            node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                            node.ID = dr["ProjectID"].ToString();
                            node.Text = dr["ProjectName"].ToString();
                            node.ChildCount = Convert.ToInt32(dr["ContractCount"]);
                            node.NodeAttributes = new Dictionary<string, string>();
                            node.NodeAttributes.Add("BatchID", context.Request.Params["BatchID"]);
                            node.NodeAttributes.Add("ProjectID", node.ID);
                            node.NodeAttributes.Add("URL", "Bu_AuditTable_List.aspx?ID=" + node.ID + "&BatchID=" + context.Request.Params["BatchID"]+"&ProjectID="+node.ID);
                            node.NodeAttributes.Add(AuditUtils.ParamName_AuditNodeType, ((int)AuditUtils.AuditTreeNodeType.Project).ToString());
                            lsNode.Add(node);
                        }
                    }
                }
                else if (nodeType == AuditUtils.AuditTreeNodeType.Project)//项目节点，加载合同
                {
                    DataTable dtBatchContract = new Honestysoft.Financing.BusinessLogic.Audit("").GetBatchContract(context.Request.Params["BatchID"], context.Request.Params["ProjectID"]);
                    if (dtBatchContract != null && dtBatchContract.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtBatchContract.Rows)
                        {
                            node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                            node.ID = dr["ContractID"].ToString();
                            node.Text = dr["ContractName"].ToString();
                            node.ChildCount = Convert.ToInt32(dr["TableCount"]);
                            node.NodeAttributes = new Dictionary<string, string>();
                            node.NodeAttributes.Add("BatchID", context.Request.Params["BatchID"]);
                            node.NodeAttributes.Add("ProjectID", context.Request.Params["ProjectID"]);
                            node.NodeAttributes.Add("ContractID", node.ID);
                            node.NodeAttributes.Add("URL", "Bu_AuditTable_List.aspx?ID=" + node.ID + "&BatchID=" + context.Request.Params["BatchID"] + "&ProjectID=" + context.Request.Params["ProjectID"] + "&ContractID=" + node.ID);
                            node.NodeAttributes.Add(AuditUtils.ParamName_AuditNodeType, ((int)AuditUtils.AuditTreeNodeType.Contract).ToString());
                            lsNode.Add(node);
                        }
                    }
                }
                //if (nodeType == Utils.ContractTreeNodeType.Root)
                //{
                //    DataTable dt = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectCategoryCount(true);
                //    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsProjectCategoryDict)
                //    {
                //        node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                //        node.ID = item.DictValue;
                //        node.Text = item.DictName;
                //        node.NodeAttributes = new Dictionary<string, string>();
                //        pageUrl = GetPageUrl(treeModule) + "?ID=" + node.ID;
                //        node.NodeAttributes.Add("URL", pageUrl);
                //        node.NodeAttributes.Add("ParentID", parentId);
                //        node.NodeAttributes.Add(Utils.ParamName_ContractNodeType, ((int)Utils.ContractTreeNodeType.ProjectCategory).ToString());
                //        if (dt != null)
                //        {
                //            DataRow[] rows = dt.Select("ProjectCategory='" + node.ID + "'");
                //            if (rows != null && rows.Length == 1)
                //            {
                //                node.ChildCount = Convert.ToInt32(rows[0]["ChildNodes"]);
                //            }
                //            else
                //            {
                //                node.ChildCount = 0;
                //            }
                //        }
                //        else
                //        {
                //            node.ChildCount = 0;
                //        }
                //        lsNode.Add(node);
                //    }
                //}
                //else if (nodeType == Utils.ContractTreeNodeType.ProjectCategory)//项目类别
                //{
                //    DataTable dtProject = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectsByCategory(parentId, true);
                //    foreach (DataRow dr in dtProject.Rows)
                //    {
                //        node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                //        node.ID = dr["ID"].ToString();
                //        node.Text = dr["SN"].ToString() + " " + dr["Name"].ToString();
                //        node.NodeAttributes = new Dictionary<string, string>();
                //        pageUrl = GetPageUrl(treeModule) + "?ID=" + node.ID;
                //        node.NodeAttributes.Add("URL", pageUrl);
                //        node.NodeAttributes.Add("ParentID", parentId);
                //        node.NodeAttributes.Add(Utils.ParamName_ContractNodeType, ((int)Utils.ContractTreeNodeType.Project).ToString());
                //        node.ChildCount = (lsContractCategoryDict == null ? 0 : lsContractCategoryDict.Count);
                //        lsNode.Add(node);
                //    }
                //}
                //else if (nodeType == Utils.ContractTreeNodeType.Project)
                //{
                //    DataTable dtContractCategoryCount = null;
                //    if (treeModule == Utils.ContractTreeModule.InitConfirm)
                //    {
                //        dtContractCategoryCount = contractBLL.GetContractCategoryCount(parentId, null);
                //    }
                //    else
                //    {
                //        dtContractCategoryCount = contractBLL.GetContractCategoryCount(parentId, true);
                //    }
                //    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsContractCategoryDict)
                //    {
                //        node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                //        node.ID = item.DictValue;
                //        node.Text = item.DictName;
                //        node.NodeAttributes = new Dictionary<string, string>();
                //        pageUrl = GetPageUrl(treeModule) + "?ID=" + node.ID + "&ParentID=" + parentId;
                //        node.NodeAttributes.Add("URL", pageUrl);
                //        node.NodeAttributes.Add("ParentID", parentId);
                //        node.NodeAttributes.Add(Utils.ParamName_ContractNodeType, ((int)Utils.ContractTreeNodeType.ContractCategory).ToString());
                //        if (treeModule == Utils.ContractTreeModule.Contract)
                //        {
                //            node.ChildCount = 0;
                //        }
                //        else
                //        {
                //            //得到合同个数
                //            if (dtContractCategoryCount != null)
                //            {
                //                node.ChildCount = dtContractCategoryCount.Select("ContractCategory='" + item.DictValue + "'").Length;
                //            }
                //            else
                //            {
                //                node.ChildCount = 0;
                //            }
                //        }
                //        lsNode.Add(node);
                //    }
                //}
                //else if (nodeType == Utils.ContractTreeNodeType.ContractCategory) //合同类别
                //{
                //    DataTable dtContracts = null;
                //    Honestysoft.Financing.BusinessLogic.Contract bll = new BusinessLogic.Contract("");
                //    if (treeModule != Utils.ContractTreeModule.InitConfirm)
                //    {
                //        dtContracts = bll.GetContracts(parentId2, parentId, true);
                //    }
                //    else
                //    {
                //        dtContracts = bll.GetContracts(parentId2, parentId, null);
                //    }
                //    foreach (DataRow dr in dtContracts.Rows)
                //    {
                //        node = new Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode();
                //        node.ChildCount = 0;
                //        node.ID = dr["ID"].ToString();
                //        node.Text = dr["Name"].ToString();
                //        node.NodeAttributes = new Dictionary<string, string>();
                //        if (treeModule == Utils.ContractTreeModule.InitConfirm)
                //        {
                //            pageUrl = "ContractConfirmInfo.aspx?ID=" + node.ID;
                //        }
                //        else
                //        {
                //            pageUrl = GetPageUrl(treeModule) + "?ID=" + node.ID;
                //        }

                //        node.NodeAttributes.Add("URL", pageUrl);
                //        node.NodeAttributes.Add("ParentID", parentId);
                //        node.NodeAttributes.Add(Utils.ParamName_ContractNodeType, ((int)Utils.ContractTreeNodeType.Contract).ToString());
                //        lsNode.Add(node);
                //    }
                //}
                //else if (nodeType == Utils.ContractTreeNodeType.Contract) //合同
                //{
                //    //if (treeModule == Utils.ContractTreeModule.DocReceive)
                //    //{
                //    //    QueryParameters query = new QueryParameters();
                //    //    query.Add("ContractID", parentId, QueryParameters.OperatorType.Equal);
                //    //    List<Bu_DocReceive> lsRec = new BaseBusinessLogic<Bu_DocReceive>("").GetEntitys(query);

                //    //}
                //}
            }
            responseData = Baseframework.WebUI.JQueryEasyUI.Tree.GetAsynTreeJson(lsNode);
            context.Response.Write(responseData);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}